#include<iostream>
#include<vector>
using namespace std;

struct ListNode {
	int val;
	ListNode *next;
	ListNode() : val(0), next(nullptr) {}
	ListNode(int x) : val(x), next(nullptr) {}
	ListNode(int x, ListNode *next) : val(x), next(next) {}
};

class Solution {
public:
	ListNode* rotateRight(ListNode* head, int k) {
		if (!head) return NULL;
		vector<ListNode *> list;
		ListNode *r = head;
		while (r) {
			list.push_back(r);
			r = r->next;
		}
		k = k % list.size();
		if (k == 0) return head;
		int index = list.size() - k;
		ListNode *newhead = list[index];
		list[index - 1]->next = NULL;
		list[list.size()-1]->next = list[0];
		return newhead;
	}
};

void main() {
	Solution s;
	ListNode root(0), n1(1), n2(2);
	root.next = &n1;
	n1.next = &n2;
	s.rotateRight(&root, 3);
}